/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */

package com.hopes.bizz.risk.setting.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.hopes.bizz.risk.setting.entity.RiskEffectEvaluationConfigEntity;
import com.hopes.bizz.risk.setting.service.RiskEffectEvaluationConfigService;
import com.hopes.boss.common.core.util.R;
import com.hopes.boss.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 双重机制运行效果评估设置
 *
 * @author pig
 * @date 2023-07-18 09:26:04
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/riskEffectEvaluationConfig" )
@Tag(description = "riskEffectEvaluationConfig" , name = "双重机制运行效果评估设置管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class RiskEffectEvaluationConfigController {

    private final RiskEffectEvaluationConfigService riskEffectEvaluationConfigService;

    /**
     * 查询
     * @return
     */
    @Operation(summary = "查询" , description = "查询" )
    @GetMapping("/list" )
    @PreAuthorize("@pms.hasPermission('risk_effect_evaluation_config_view')" )
    public R getriskEffectEvaluationConfigList() {
        return R.ok(riskEffectEvaluationConfigService.list());
    }

    /**
     * 新增双重机制运行效果评估设置
     * @param riskEffectEvaluationConfig 双重机制运行效果评估设置
     * @return R
     */
    @Operation(summary = "新增双重机制运行效果评估设置" , description = "新增双重机制运行效果评估设置" )
    @SysLog("新增双重机制运行效果评估设置" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('risk_effect_evaluation_config_add')" )
    public R save(@RequestBody RiskEffectEvaluationConfigEntity riskEffectEvaluationConfig) {
        List<RiskEffectEvaluationConfigEntity> riskEffectEvaluationConfigEntities = riskEffectEvaluationConfigService.list();
        if (CollectionUtil.isNotEmpty(riskEffectEvaluationConfigEntities)){
            riskEffectEvaluationConfigService.removeById(riskEffectEvaluationConfigEntities.get(0).getId());
        }
        return R.ok(riskEffectEvaluationConfigService.save(riskEffectEvaluationConfig));
    }
}
